#include "debug_uart.h"
#include "timer0.h"
#include <stdarg.h>
#include <stdio.h>

/* USART RX Port/Pin definition */
#define USART3_RX_PORT                   (PortB)
#define USART3_RX_PIN                    (Pin03)
#define USART3_RX_FUNC                   (Func_Usart3_Rx)

/* USART TX Port/Pin definition */
#define USART3_TX_PORT                   (PortB)
#define USART3_TX_PIN                    (Pin04)
#define USART3_TX_FUNC                   (Func_Usart3_Tx)

void Message( const char*  fmt, ... ){
	va_list  ap;
	va_start( ap, fmt );
	vprintf( fmt, ap );
	va_end( ap );
}

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
	
PUTCHAR_PROTOTYPE{
	while((M4_USART3->SR & (1<<7))==0){;}
	M4_USART3->DR_f.TDR = (uint8_t)ch;

	return (uint8_t)(ch);
}



/**
 *******************************************************************************
 ** \brief USART RX error irq callback function.
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
static void Usart3ErrIrqCallback(void){
    if (Set == USART_GetStatus(M4_USART3, UsartFrameErr)){
        USART_ClearStatus(M4_USART3, UsartFrameErr);
    }
    else{
    }
    if (Set == USART_GetStatus(M4_USART3, UsartParityErr)){
        USART_ClearStatus(M4_USART3, UsartParityErr);
    }
    else{	
    }
    if (Set == USART_GetStatus(M4_USART3, UsartOverrunErr)){
        USART_ClearStatus(M4_USART3, UsartOverrunErr);
    }
    else{	
    }
}


void USART3_Init(uint32_t baud){
	uint16_t u16RxData;
	stc_irq_regi_conf_t stcIrqRegiCfg;
	en_result_t enRet = Ok;
	const stc_usart_uart_init_t stcInitCfg = {
		UsartIntClkCkNoOutput,
		UsartClkDiv_1,
		UsartDataBits8,
		UsartDataLsbFirst,
		UsartOneStopBit,
		UsartParityNone,
		UsartSampleBit8,
		UsartStartBitFallEdge,
		UsartRtsEnable,
	};
	/* Enable peripheral clock */
	PWC_Fcg1PeriphClockCmd(PWC_FCG1_PERIPH_USART3, Enable);
	/* Initialize USART IO */
	PORT_SetFunc(USART3_RX_PORT, USART3_RX_PIN, USART3_RX_FUNC, Disable);
	PORT_SetFunc(USART3_TX_PORT, USART3_TX_PIN, USART3_TX_FUNC, Disable);
	/* Initialize UART */
	enRet = USART_UART_Init(M4_USART3, &stcInitCfg);
	if (enRet != Ok){
		while (1){
		}
	}
    /* Set baudrate */
    enRet = USART_SetBaudrate(M4_USART3, baud);
    if (enRet != Ok){
        while (1){
        }
    }
    /* Set USART RX error IRQ */
    stcIrqRegiCfg.enIRQn = Int010_IRQn;
    stcIrqRegiCfg.pfnCallback = &Usart3ErrIrqCallback;
    stcIrqRegiCfg.enIntSrc = INT_USART3_EI;
    enIrqRegistration(&stcIrqRegiCfg);
    NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
    NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
    NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);		
    /*Enable RX && TX function*/
    USART_FuncCmd(M4_USART3, UsartRx, Enable);
    USART_FuncCmd(M4_USART3, UsartTx, Enable);
	USART_FuncCmd(M4_USART3, UsartRxInt, Enable);
}

void USART3_Send(uint8_t *buf,uint16_t len){
	uint16_t i=0;
	for(i=0;i<len;i++){	
		while (Reset == USART_GetStatus(M4_USART3, UsartTxEmpty));
		USART_SendData(M4_USART3,buf[i]);
		
	}
}


